package com.example.hotel_lxy.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.hotel_lxy.bean.SqliteBean;

import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private UserSQLiteOpenHelper helper;

    public UserDao(Context context) {
        helper = new UserSQLiteOpenHelper(context, "Blcs", null, 1);
    }

    /**
     * 添加一条记录
     * @param name 手机号
     * @param phone 密码
     * @return 返回的是添加后在数据库的行号  -1代表添加失败
     */
    public long add(String name, String phone, String category_id,String avatar,String mobile,String token){
        SQLiteDatabase db = helper.getWritableDatabase();
        //db.execSQL("insert into contactinfo (name,phone) values (?,?)", new Object[]{name,phone});
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("address", phone);
        contentValues.put("ids", category_id);
        contentValues.put("avatar",avatar);
        contentValues.put("mobile",mobile);
        contentValues.put("token",token);
        //内部是组拼sql语句实现的.
        long rowid = db.insert("SqliteDemo", null, contentValues);
        //记得释放数据库资源
        db.close();
        return rowid;
    }

    /**
     * 修改联系人电话号码
     * @param name 修改人的电话号码
     * @param avatar 要修改的图片
     * @return 0代表一行也没有更新成功, >0 整数代表的是更新了多少行记录
     */
    public int updateavatar(String name,String avatar){
        //根据姓名修改电话号码
        SQLiteDatabase db = helper.getWritableDatabase();
        //db.execSQL("update contactinfo set phone =? where name=?", new Object[]{newphone,name});
        ContentValues values = new ContentValues();
        values.put("avatar",avatar);
        int rowcount =  db.update("SqliteDemo", values, "name=?", new String[]{name});
        db.close();
        return rowcount;
    }

    /**
     * 根据姓名删除一条记录
     * @param name 要删除的联系人的姓名
     * @return 返回0代表的是没有删除任何的记录 返回整数int值代表删除了几条数据
     */
    public int delete(String name){
        //判断这个数据是否存在.
        SQLiteDatabase db = helper.getWritableDatabase();
        //db.execSQL("delete from contactinfo where name=?", new Object[]{name});
        int rowcount = db.delete("contactinfo2", "name=?", new String[]{name});
        db.close();
        //再从数据库里面查询一遍,看name是否还在
        return rowcount;
    }
    /**
     * 修改联系人电话号码
     * @param name 新的电话号码
     * @param name 要修改的联系人姓名
     * @return 0代表一行也没有更新成功, >0 整数代表的是更新了多少行记录
     */
    public int update(String name, String phone, String category_id,String avatar,String mobile,String token){
        //根据姓名修改电话号码
        SQLiteDatabase db = helper.getWritableDatabase();
        //db.execSQL("update contactinfo set phone =? where name=?", new Object[]{newphone,name});
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("address", phone);
        values.put("ids", category_id);
        values.put("avatar",avatar);
        values.put("mobile",mobile);
        values.put("token",token);
        int rowcount =  db.update("SqliteDemo", values, "name=?", new String[]{name});
        db.close();
        return rowcount;
    }
    /**
     * 查询联系人的电话号码
     * @param name 要查询的联系人
     * @return 电话号码
     */
    public String getPhoneNumber(String name){
        String phone = null;
        SQLiteDatabase db = helper.getReadableDatabase();
        //Cursor  cursor = db.rawQuery("select phone from contactinfo where name=?", new String[]{name});
        Cursor cursor =  db.query("SqliteDemo", new String[]{"name"}, "name=?", new String[]{name}, null, null, null);
        if(cursor.moveToNext()){//如果光标可以移动到下一位,代表就是查询到了数据
            phone = cursor.getString(0);
        }
        cursor.close();//关闭掉游标,释放资源
        db.close();//关闭数据库,释放资源
        return phone;
    }


    /**
     * 获取所有数据
     * @return
     */
    public  List<SqliteBean> getAll(){
        List<SqliteBean> mlist=new ArrayList<>();
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor =  db.query("SqliteDemo", new String[]{"name", "address", "ids","avatar","mobile","token"}, null, null, null, null, null);
        while (cursor.moveToNext()){//如果光标可以移动到下一位,代表就是查询到了数据
            SqliteBean sqliteBean=new SqliteBean();
            sqliteBean.setName(cursor.getString(0));
            sqliteBean.setAddress(cursor.getString(1));
            sqliteBean.setIds(cursor.getString(2));
            sqliteBean.setAvatar(cursor.getString(3));
            sqliteBean.setMobile(cursor.getString(4));
            sqliteBean.setToken(cursor.getString(5));
            mlist.add(sqliteBean);
        }
        cursor.close();//关闭掉游标,释放资源
        db.close();//关闭数据库,释放资源
        return mlist;
    }

    /**
     * 查询所有表名
     * @return
     */
    public String updateField(){
        String  allTable="";
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select name from sqlite_master where type='table' order by name", null);
        while(cursor.moveToNext()){
            //遍历出表名
            String name = cursor.getString(0);
            allTable += name+"....";
        }

        return allTable;
    }

}
